// subHoliday/signInList/sign_in_list.js
const appg = getApp().globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    option1: [{
      text: '全部课程',
      value: null
    }, ],
    option2: [{
        text: '默认',
        value: null
      },
      {
        text: '已签到',
        value: 1
      },
      {
        text: '未签到',
        value: 0
      },
    ],
    value1: null,
    value2: null,
    // key_value: null,
    page: {
      pageSize: 10,
      pageNum: 1,
      totalCount: 0
    },
    uid: null,
    identity_ID: null,
    signInList: [],
  },
  // 获取签到列表
  async getSignInList() {
    wx.showLoading({
      title: '加载中',
    })
    try {
      const params = {
        uid: this.data.uid,
        ...this.data.page,
        identity_ID: this.data.identity_ID,
        chat_id: this.data.value1,
        status: this.data.value2,
        school_ID:appg.userInfo.school_ID
      }
      const json = await getApp().$axios("/getSignList", "POST", params)
      wx.hideLoading()
      if (json.status !== 200) {
        wx.showToast({
          title: json.data,
          icon: 'error'
        })
        return
      }
      console.log(json.data);
      this.setData({
        signInList: [...this.data.signInList, ...json.data],
        page: json.page
      })
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  change1(e) {
    const id = e.detail
    this.setData({
      value1: id,
      signInList: [],
      page: {
        pageSize: 10,
        pageNum: 1,
        totalCount: 0
      }
    })
    this.getSignInList()
  },
  change2(e) {
    const id = e.detail
    this.setData({
      page: {
        pageSize: 10,
        pageNum: 1,
        totalCount: 0
      },
      value2: id,
      signInList: []
    })
    this.getSignInList()
  },
  // 修改签到状态  成功
  async changeSign_yes(e) {
    const {
      uid,
      sid
    } = e.currentTarget.dataset;
    try {
      const json = await getApp().$axios("/changeSignInfo_v2", 'POST', {
        uid,
        sid,
        status: 1
      })
      if (json.status !== 200) {
        wx.showToast({
          title: '修改失败，请重试',
          icon: 'none'
        })
        return
      }
      this.setData({
        page: {
          pageSize: 10,
          pageNum: 1,
          totalCount: 0
        },
        signInList: []
      })
      this.getSignInList()
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  // 失败
  async changeSign_none(e) {
    const {
      uid,
      sid
    } = e.currentTarget.dataset;
    try {
      const json = await getApp().$axios("/changeSignInfo_v2", 'POST', {
        uid,
        sid,
        status: 0
      })
      if (json.status !== 200) {
        wx.showToast({
          title: '修改失败，请重试',
          icon: 'none'
        })
        return
      }
      this.setData({
        page: {
          pageSize: 10,
          pageNum: 1,
          totalCount: 0
        },
        signInList: []
      })
      this.getSignInList()
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  // 获取加入群聊的列表
  async getGroupList() {
    try {
      const params = {
        uid: this.data.uid,
        pageNum: 1,
        pageSize: 999
      }
      const json = await getApp().$axios("/getGroupChatList", "POST", params)
      if (json.status !== 200) {
        wx.showToast({
          title: json.data,
          icon: 'error'
        })
        return
      }
      json.data.forEach(item => {
        const obj = {
          text: item.chat_name,
          value: item.id
        }
        this.data.option1.push(obj)
      });
      this.setData({
        option1: this.data.option1
      })
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  // 加载更多
  loadMore() {
    this.setData({
      'page.pageNum': this.data.page.pageNum + 1
    })
    this.getSignInList()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      uid: appg.userInfo.id,
      identity_ID: appg.userInfo.identity_ID
    })
    this.getGroupList()
    this.getSignInList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})